package com.moleq.dialog;

import java.sql.Date;
import java.util.Calendar;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.SaleActivity;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class DateSettingSelectionDialog extends MoleQBaseDialog {

	private Button btnConfirmDate;
	private Button btnClose;
	private TextView tv_datetime;
	private DatePicker dp;
	private String age_time;
	private int viewid;
	private OnResultListener mOnResultListener;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			
			switch (arg0.getId()) {
			case R.id.btn_date_Confirm:
				onCloseWithReturn(viewid,age_time);
				break;
			case R.id.btn_date_Close:
				dismiss();
				break;
			}	
		}
	};
	


	public DateSettingSelectionDialog(Context context) {
		super(context, R.style.dialog, context);
		constructor(context);
	}
	
	public DateSettingSelectionDialog(Context context,Object base) {
		super(context, R.style.dialog, base);
		constructor(context);
		constructor(context,base);
	}

	private void constructor(Context context) {
		mContext = context;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}
	

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_setting_date_selection);
		this.setCancelable(false);

		btnConfirmDate = (Button) findViewById(R.id.btn_date_Confirm);
		btnConfirmDate.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.btn_date_Close);
		btnClose.setOnClickListener(mOnClickListener);

		tv_datetime = (TextView) findViewById(R.id.tv_datetime);
		dp = (DatePicker) findViewById(R.id.dps_date);
	}

	public void show(int vid,String dateText) {
		super.show();
		viewid = vid;
		
		tv_datetime.setText(MoleQUtil.getNowwithFormat("yyyy-MM-dd"));
		age_time = MoleQUtil.getNowwithFormat("yyyy-MM-dd");
		
		Calendar calendar=Calendar.getInstance();
		if (!dateText.equals("")) {
        	calendar.setTime(MoleQUtil.ChangeDate(dateText));
        	age_time = dateText;
        	tv_datetime.setText(dateText);
		}
		
		int monthOfYear= calendar.get(Calendar.MONTH);
        int year=calendar.get(Calendar.YEAR);
        int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        
		dp.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

				age_time = year + "-" + MoleQUtil.fixNumber3(monthOfYear + 1, "00") + "-" + MoleQUtil.fixNumber3(dayOfMonth, "00");
				tv_datetime.setText(age_time);

			}

		});

	}

}
